package com.qzdsoft.erpcloud.controller.act;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.controller.stock.StockPuOutBoundController;
import com.qzdsoft.erpcloud.domain.act.CenterCompanyProduct;
//import com.qzd.erpcloud.domain.help.RedisHelper;
import com.qzdsoft.erpcloud.service.act.CenterCompanyProductService;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.act.CenterProductService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.vo.query.act.CenterCompanyProductQueryParam;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;

/**
 * Created by Yang
 */
@Controller
@RequestMapping("/act/center/company/product")
public class CenterCompanyProductController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(CenterCompanyProductController.class);

    @Autowired
    private CenterCompanyProductService actCenterCompanyProductService;

//    @Autowired
//    private RedisHelper redisHelper;
    
    @Autowired
    CenterCompanyService actCenterCompanyService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    CenterProductService actCenterProductService;
    
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
//        model.addAttribute("actCenterCompanyMap", redisHelper.getActCenterCompanyMap());
        model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
//        model.addAttribute("sysRoleMap", redisHelper.getSysRoleMap());
        model.addAttribute("sysRoleMap", sysRoleService.getAllRoleList());
//        model.addAttribute("actCenterProductMap", redisHelper.getActCenterProductMap());
        model.addAttribute("actCenterProductMap", actCenterProductService.selectAll());
        return "act/company_product/company_product";
    }
    
    /**
     * 查询数据列表
     * @param request
     * @param param
     * @return
     */
    @ResponseBody
    @SysLog(description="充值记录查询")
    @RequestMapping("/data")
    public LayTableResponse<CenterCompanyProduct> data(HttpServletRequest request, CenterCompanyProductQueryParam param) {
//        Integer companyId = SessionUtil.getLoggedCompanyId(request);
        Integer companyId = param.getCompanyId();
        
        if(null != companyId){
            param.setCompanyId(companyId);
        }

        return actCenterCompanyProductService.selectData(param.getProductId(), param.getCompanyId(), param.getRoleId(),param.getTimeFrom(),param.getTimeTo(), param.getPage(), param.getLimit());
    }

    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer CompanyId,Integer id) {
        
        ModelAndView mv = new ModelAndView();
        
        mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
//      model.addAttribute("sysRoleMap", redisHelper.getSysRoleMap());
        mv.addObject("sysRoleMap", sysRoleService.getAllRoleList());
//      model.addAttribute("actCenterProductMap", redisHelper.getActCenterProductMap());
        mv.addObject("actCenterProductMap", actCenterProductService.selectAll());
        if(id == null) {
            mv.setViewName("act/company_product/add");
        }else {
            CenterCompanyProduct centerCompanyProduct = actCenterCompanyProductService.selectById(id);
            mv.addObject("centerCompanyProduct", centerCompanyProduct);
            mv.setViewName("act/company_product/edit");
        }
        return mv;
    }

    /**
     *
     * @param request
     * @param product
     * @return
     */
    @ResponseBody
    @SysLog(description="充值记录修改保存")
    @RequestMapping("/edit_execute")
    public Response<String> editExecute(CenterCompanyProduct product) {
//        product.setCompany_id(SessionUtil.getLoggedCompanyId(request, product.getCompany_id()));
        return actCenterCompanyProductService.saveOrUpd(product);
    }
    /**
     *
     * @param request
     * @param product
     * @return
     */
    @ResponseBody
    @SysLog(description="充值记录新增保存")
    @RequestMapping("/create_execute")
    public Response<String> createExecute(CenterCompanyProduct product) {
//        product.setCompany_id(SessionUtil.getLoggedCompanyId(request, product.getCompany_id()));
        return actCenterCompanyProductService.saveOrUpd(product);
    }
    
    /**
     * 根据id删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="充值记录删除")
    @RequestMapping("/del_execute")
    public Response<String> del(HttpServletRequest request, @RequestParam("id") Integer id) {
        return actCenterCompanyProductService.del(id);
    }
    /**
     * 查看明细
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView();
        CenterCompanyProduct centerCompanyProduct = actCenterCompanyProductService.selectById(id);
        mv.addObject("centerCompanyProduct", centerCompanyProduct);
        mv.setViewName("act/company_product/view");
        return mv;
    }
    
}
